package br.ucb.tcc.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;

import thinlet.swing.border.LineBorder;
import thinlet.swing.container.ToolFooter;
import thinlet.swing.table.Cell;
import thinlet.swing.table.Table;
import br.ucb.tcc.entidades.Perfil;
import br.ucb.tcc.persistencia.TwitterDAO;

public class ListarUsuarioBancoPanel extends AbstractPanel implements Table.Listener<Perfil>, ActionListener {
	
	private Table<Perfil> lista;
	private ToolFooter subPanelHeader;
	private JScrollPane scrollPane;
	
	
	public ListarUsuarioBancoPanel() {
		super(new BorderLayout());
		JPanel header = new JPanel(new BorderLayout());
		subPanelHeader = new ToolFooter();
		JLabel lbHeader = new JLabel("<html><body>" + "<table><tr><td>" + "<img width='650' height='100' src='" + imgsrc + "'></img>" + "</td><td><h3>Listagem de usuários</h3></td></tr></table></body></html>");
		lbHeader.setBorder(new LineBorder(Color.lightGray, 0, 0, 1, 0, 0, 8, 8, 8));
		lbHeader.setBackground(new Color(0xdddddd));
		lbHeader.setOpaque(true);
		header.add(lbHeader, BorderLayout.CENTER);
		header.add(subPanelHeader, BorderLayout.SOUTH);
		add(header, BorderLayout.NORTH);
		popularLista();
		scrollPane = new JScrollPane(lista);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		add(scrollPane, BorderLayout.CENTER);
		updateLista(new TwitterDAO().buscarTodosPerfis());
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doubleClicked(Perfil perfilDTO) {
		JLabel lbHeader = new JLabel(montarDetalhamento(perfilDTO));
		lbHeader.setBorder(new LineBorder(Color.lightGray, 0, 0, 1, 0, 0, 8, 8, 8));
		lbHeader.setBackground(new Color(0xdddddd));
		lbHeader.setOpaque(true);
		JOptionPane.showMessageDialog(null, lbHeader, "Detalhamento de Usuário", JOptionPane.PLAIN_MESSAGE, null);  
	}
	
	
	public void popularLista() {
		lista = new Table<Perfil>();
		lista.setCells(Perfil.class, new Cell("nomePerfil").setColor(new Color(0x4a77ce)),  new Cell("localizacao").setColor(new Color(0x4a77ce)) , new Cell("ultimoPost").setColor(new Color(0x4a77ce)));
		lista.addListListener(this);
	}
	
	public void updateLista(List<Perfil> perfis) {
		popularLista();
		this.remove(scrollPane);
		for (Perfil perfilDTO : perfis) {
			lista.addItem(perfilDTO);
		}
		scrollPane = new JScrollPane(lista);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		add(scrollPane, BorderLayout.CENTER);
		this.revalidate();
	}
	
	public void limparLista() {
		lista.setModel(null);
		lista.repaint();
		this.revalidate();
	}
	
	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
		}
		return scrollPane;
	}


	public ToolFooter getSubPanelHeader() {
		if (subPanelHeader == null) {
			subPanelHeader = new ToolFooter();
		}
		
		return subPanelHeader;
	}


	@Override
	public void selectionChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
